package com.antexpress.controller;

import com.antexpress.bean.FormatValidate;
import com.antexpress.common.Const;
import com.antexpress.common.Response;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.Title;
import com.antexpress.service.DepartmentService;
import com.antexpress.service.EmployeeService;
import com.antexpress.util.DataUtil;
import com.antexpress.util.MD5Util;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Andy
 */
@Controller
@RequestMapping(value="/emps/")
public class EmpManagementController {
    @Autowired
    private EmployeeService empService;
    @Autowired
    private DepartmentService deptService;
    
    /*还没有写权限判断*/
    @RequestMapping(value="list_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listEmp(String name, String mobile, String dept, String title, int pageNo, HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        resp = empService.empForPage(name, mobile, dept, title, pageNo, 10);
        return resp;
    }
    
    @RequestMapping(value="add_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response addEmp(String username, String password, 
            String name, String gender, String titleId, String mobile, String email,
            Date birth, String salary, HttpSession session){
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        Employee newEmp = new Employee();
        if(!FormatValidate.checkUsername(username)){
            resp = Response.createByErrorMessage("Username is not conformed to the regulations!");
        }else if(!FormatValidate.checkPassword(password)){
            resp = Response.createByErrorMessage("Password is not conformed to the regulations!");
        }else if(empService.isUsernameExisted(username)){
            resp = Response.createByErrorMessage("Username has been used!");
        }else if(name==null||name.equals("")){
            resp = Response.createByErrorMessage("Please input the name!");
        }else if(!mobile.equals("")&&!FormatValidate.isPhoneLegal(mobile))
            resp = Response.createByErrorMessage("Mobile is not conformed to the regulations!");
        else if(!email.equals("")&&!FormatValidate.checkEmail(email))
                resp = Response.createByErrorMessage("Emial is not conformed to the regulations!");
        else if(!FormatValidate.isBirthAllowed(birth))
            resp = Response.createByErrorMessage("Age must be more than 18 years old!");
        else{
            newEmp.setUsername(username);
            newEmp.setPassword(MD5Util.MD5EncodeUtf8(password));
            newEmp.setName(name);
            boolean isEmailExisted = false;
            if(email!=null&&!email.equals("")){
                isEmailExisted = empService.isEmailExisted(email);
                if(isEmailExisted){
                    resp = Response.createByErrorMessage("Sorry, the email has been used!");
                    return resp;
                }else{
                    newEmp.setEmail(email);
                }
            }else { //没有设置邮箱
                newEmp.setEmail(null);
            }
            if(titleId!=null&&!titleId.equals("")){
                Title title = deptService.getTitleById(titleId);
                if(title!=null)
                    newEmp.setTitle(title);
                else
                    return Response.createByErrorMessage("Title is not existed!");
            }else{
                return Response.createByErrorMessage("Please select a department and title!");
            }
            if(gender.equals("f"))
                newEmp.setGender("f");
            else
                newEmp.setGender("m");
            try{
                double sal = Double.valueOf(salary);
                newEmp.setSalary(sal);
            }catch(Exception e){
                return Response.createByErrorMessage("Salary is not conformed to the regulations!");
            }
            newEmp.setBirth(birth);
            newEmp.setMobile(mobile);
            newEmp.setId(DataUtil.getUUID());
            newEmp.setStatus(0);
            newEmp.setCreatetime(new Date());
            newEmp.setUpdatetime(new Date());
            resp = empService.addEmp(newEmp);
        }
        return resp;
    }
    
    @RequestMapping(value="edit_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response editEmp(String id, String password, String name, String gender, 
            String titleId, String mobile, String email, Date birth, 
            String salary, int status, HttpSession session){
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        Employee emp = null;
        Response resp = null;
        if(currentEmp==null){
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        if(id!=null&&!id.equals("")){
            emp = empService.getEmpById(id);
            if(emp==null)
                return Response.createByErrorMessage("Employee isn't existed!");
        }else{
            return Response.createByErrorMessage("Employee isn't existed!");
        }
        if(password!=null&&!password.equals("")&&!FormatValidate.checkPassword(password)){
                return Response.createByErrorMessage("Password is not conformed to the regulations!");
        }else if(name==null||name.equals("")){
            resp = Response.createByErrorMessage("Please input the name!");
        }else if(!mobile.equals("")&&!FormatValidate.isPhoneLegal(mobile))
            resp = Response.createByErrorMessage("Mobile is not conformed to the regulations!");
        else if(!email.equals("")&&!FormatValidate.checkEmail(email))
            resp = Response.createByErrorMessage("Emial is not conformed to the regulations!");
        else if(!FormatValidate.isBirthAllowed(birth))
            resp = Response.createByErrorMessage("Age must be more than 18 years old!");
        else if(!FormatValidate.checkEmpStatus(status))
            resp = Response.createByErrorMessage("Status is not allowed!");
        else{
            if(password!=null&&!password.equals(""))
                emp.setPassword(MD5Util.MD5EncodeUtf8(password));
            emp.setName(name);
            if(gender.equals("f"))
                emp.setGender("f");
            else
                emp.setGender("m");
            if(titleId!=null&&!titleId.equals("")){
                Title title = deptService.getTitleById(titleId);
                if(title!=null)
                    emp.setTitle(title);
                else
                    return Response.createByErrorMessage("Title is not existed!");
            }else{
                return Response.createByErrorMessage("Please select a department and title!");
            }
            boolean isEmailExisted = false;
            if(email!=null&&!email.equals("")){
                if(!email.equals(emp.getEmail()))
                    isEmailExisted = empService.isEmailExisted(email);
                if(isEmailExisted){
                    resp = Response.createByErrorMessage("Sorry, the email has been used!");
                    return resp;
                }else{
                    emp.setEmail(email);
                }
            }else { //没有设置邮箱
                emp.setEmail(null);
            }
            emp.setMobile(mobile);
            emp.setBirth(birth);
            try{
                double sal = Double.valueOf(salary);
                emp.setSalary(sal);
            }catch(Exception e){
                return Response.createByErrorMessage("Salary is not conformed to the regulations!");
            }
            emp.setStatus(status);
            emp.setUpdatetime(new Date());
            resp = empService.updateProfile(emp);
        }
        return resp;
    }
    
    @RequestMapping(value="delete_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response deleteEmp(String id, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        if(id!=null&&!id.equals("")){
            if(id.equals(emp.getId()))
                return Response.createByErrorMessage("Sorry, it's not allowed to delete yourself!");
            Employee delEmp = empService.getEmpById(id);
            if(delEmp!=null){
                delEmp.setStatus(-10);
                delEmp.setUpdatetime(new Date());
                resp = empService.updateProfile(delEmp);
            }else{
                resp = Response.createByErrorMessage("Please select a valid employee!");
            }
        }else{
            resp = Response.createByErrorMessage("Please select a valid employee!");
        }
        return resp;
    }
    
    @RequestMapping(value="batch_delete_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response batchDeleteEmp(String[] ids, HttpSession session) {
        Response resp = null;
        Employee currentEmp = (Employee)session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        if(ids==null||ids.length<=0)
            return Response.createByErrorMessage("Please select some employees!");
        for(String id:ids){
            if(id.equals(currentEmp.getId()))
                return Response.createByErrorMessage("Sorry, it's not allowed to delete yourself!");
        }
        resp = empService.batchDeleteEmp(ids);
        return resp;
    }
    
    @RequestMapping(value="home_emp.do", method=RequestMethod.POST)
    @ResponseBody
    public Response getHomeEmp(){
        return empService.getHomeEmpMsg();
    }
}
